package com.albert.iotcar;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import com.albert.View.GameButton;
import com.albert.View.IOSSwitch;
import com.albert.WSMqtt.WSMqttService;
import com.albert.model.DeviceEnableModel;
import com.albert.model.OperateCameraModel;
import com.albert.model.OperateCarModel;
import com.albert.model.PackageModel;
import com.albert.tool.CameraActionType;
import com.albert.tool.CarActionType;
import com.albert.tool.JsonUtils;
import com.albert.tool.MessageType;

public class CallActivityMoveCamera extends FrameLayout {
    private static final String TAG = "移动摄像头";
    private String mqttReceiveId = "";
    private WSMqttService wsMqttService;
    private IOSSwitch iosSwitch;

    public CallActivityMoveCamera(Context context) {
        super(context);
        init(context);
    }

    public CallActivityMoveCamera(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CallActivityMoveCamera(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void setMqttService(WSMqttService mqttService) {
        this.wsMqttService = mqttService;
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.activity_call_move_camera, this, true);        iosSwitch = findViewById(R.id.iosSwitch);
        iosSwitch.setOn(false);
        iosSwitch.setOnClickListener(v -> {
            boolean status = iosSwitch.isOn();
            PackageModel baseModel = new PackageModel(MessageType.PHONE_SET_DEVICE_ENABLE, mqttReceiveId,
                    new DeviceEnableModel(status));
            String testMsg = JsonUtils.objectToJSonStr(baseModel);
            wsMqttService.publishMessage(testMsg);
        });

        GameButton upButton = findViewById(R.id.move_camera_up);
        // 设置动作回调
        upButton.setOnRepeatListener((isPressed) -> {
            Log.d(TAG, "up");
            PackageModel baseModel = new PackageModel(MessageType.PHONE_MOVE_CAMERA, mqttReceiveId,
                    new OperateCameraModel(CameraActionType.CAMERA_ACTION_UP, isPressed));
            String testMsg = JsonUtils.objectToJSonStr(baseModel);
            wsMqttService.publishMessage(testMsg);
        });

        GameButton downButton = findViewById(R.id.move_camera_down);
        // 设置动作回调
        downButton.setOnRepeatListener((isPressed) -> {
            Log.d(TAG, "down");
            PackageModel baseModel = new PackageModel(MessageType.PHONE_MOVE_CAMERA, mqttReceiveId,
                    new OperateCameraModel(CameraActionType.CAMERA_ACTION_DOWN, isPressed));
            String testMsg = JsonUtils.objectToJSonStr(baseModel);
            wsMqttService.publishMessage(testMsg);
        });

        GameButton leftButton = findViewById(R.id.move_camera_left);
        // 设置动作回调
        leftButton.setOnRepeatListener((isPressed) -> {
            Log.d(TAG, "left");
            PackageModel baseModel = new PackageModel(MessageType.PHONE_MOVE_CAMERA, mqttReceiveId,
                    new OperateCameraModel(CameraActionType.CAMERA_ACTION_LEFT, isPressed));
            String testMsg = JsonUtils.objectToJSonStr(baseModel);
            wsMqttService.publishMessage(testMsg);
        });

        GameButton rightButton = findViewById(R.id.move_camera_right);
        // 设置动作回调
        rightButton.setOnRepeatListener((isPressed) -> {
            Log.d(TAG, "right");
            PackageModel baseModel = new PackageModel(MessageType.PHONE_MOVE_CAMERA, mqttReceiveId,
                    new OperateCameraModel(CameraActionType.CAMERA_ACTION_RIGHT, isPressed));
            String testMsg = JsonUtils.objectToJSonStr(baseModel);
            wsMqttService.publishMessage(testMsg);
        });
    }
}